package com.shixun.milktea.config;

import com.shixun.milktea.converter.DateFormatter;
import com.shixun.milktea.converter.StringFormatter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author Zourongsheng
 * @version 1.0
 * @date 2022/6/1 13:43
 */
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {


    /**
     * 跨域处理
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //添加映射
        registry.addMapping("/**")
                .allowCredentials(true)//是否发现cookie
                .allowedOriginPatterns("*")//设置放行哪些原始域
                .allowedMethods("*")//设置放行哪些请求方式
                .allowedHeaders("*")//设置放行哪些原始请求头信息
                .exposedHeaders("*");//暴露哪些原始请求头信息
    }
    //注册自定义SpringMVC的类型转换器
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(new StringFormatter());
        registry.addFormatter(new DateFormatter());
    }
    /**
     * 目前是解决的是swagger无法被搜索到的问题
     * @author zrs
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决 swagger-ui.html 404报错
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        // 解决 doc.html 404 报错
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/swagger/**").addResourceLocations("classpath:/static/swagger/");
        registry.addResourceHandler("/v2/api-docs/**").addResourceLocations("classpath:/static/swagger/");

    }







}
